package com.ktg.mes.rec.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.rec.mapper.RecProcessesExtMapper;
import com.ktg.mes.rec.domain.RecProcessesExt;
import com.ktg.mes.rec.service.IRecProcessesExtService;



/**
 * 工序项Service业务层处理
 *
 * @author byteco
 * @date 2022-11-04
 */
@Service
public class RecProcessesExtServiceImpl extends ServiceImpl<RecProcessesExtMapper, RecProcessesExt> implements IRecProcessesExtService
{
    @Autowired
    private RecProcessesExtMapper recProcessesExtMapper;



    /**
     * 查询工序项
     *
     * @param entity 工序项
     * @param page 分页对象
     * @return 工序项
     */
    @Override
    public IPage<RecProcessesExt> queryPageList(RecProcessesExt entity, Page<RecProcessesExt> page) {
        LambdaQueryWrapper<RecProcessesExt> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(RecProcessesExt::getCreateTime);
        IPage<RecProcessesExt> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询工序项
     *
     * @param entity 工序项
     * @return 工序项
     */
    @Override
    public List<RecProcessesExt> queryList(RecProcessesExt entity) {
        LambdaQueryWrapper<RecProcessesExt> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(RecProcessesExt::getCreateTime);
        return this.list(lqw);
    }

    public RecProcessesExt getInfo(Long id)
    {
        QueryWrapper<RecProcessesExt> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RecProcessesExt::getId, id);
        return this.getOne(queryWrapper);
    }


    /**
     * 新增工序项
     *
     * @param entity 工序项
     * @return 结果
     */
    @Override
    public void insert(RecProcessesExt entity)
    {
        setEntityData(entity);
        this.save(entity);
    }

    private void setEntityData(RecProcessesExt entity){
        String userName = SecurityUtils.getUsername();
        if (entity.getCreateBy() == null){
            entity.setCreateBy(userName);
        }
        if (entity.getCreateTime() == null){
            entity.setCreateTime(new Date());
        }
        entity.setUpdateBy(userName);
        entity.setUpdateTime(new Date());
        if (entity.getRulePda() == null || entity.getRulePda().length() < 1){
            entity.setRulePda("{}");
        }
        if (entity.getRuleBackstage() == null || entity.getRuleBackstage().length() < 1){
            entity.setRuleBackstage("{}");
        }
        if (entity.getOrderBackstage() == null || entity.getOrderBackstage() < 1){
            entity.setOrderBackstage(0);
        }

    }


    /**
     * 修改工序项
     *
     * @param entity 工序项
     * @return 结果
     */
    @Override
    public boolean update(RecProcessesExt entity)
    {
        setEntityData(entity);
        return this.updateById(entity);
    }

    /**
     * 批量删除工序项
     *
     * @param ids 需要删除的工序项主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] ids)
    {

        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 删除工序项信息
     *
     * @param id 工序项主键
     * @return 结果
     */
    @Override
    public boolean delete(Long id)
    {
        return this.removeById(id);
    }

    private LambdaQueryWrapper<RecProcessesExt> buildQueryWrapper(RecProcessesExt entity) {
        LambdaQueryWrapper<RecProcessesExt> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(entity.getNamePda()), RecProcessesExt::getNamePda, entity.getNamePda());
        lqw.eq(StringUtils.isNotBlank(entity.getDes()), RecProcessesExt::getDes, entity.getDes());
        lqw.eq(entity.getRequired() != null, RecProcessesExt::getRequired, entity.getRequired());
        lqw.eq(StringUtils.isNotBlank(entity.getType()), RecProcessesExt::getType, entity.getType());
        lqw.eq(entity.getProcessId() != null, RecProcessesExt::getProcessId, entity.getProcessId());
        return lqw;
    }
}
